package prosjekt;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.TimerTask;
import java.util.Vector;
import java.util.Timer;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Basic panel with a list of undelivered orders and info panel
 * @author Henning
 *
 */
@SuppressWarnings("serial")
public class LeveringsForm extends JPanel {
	Vector<Integer> ordreVektor;
	JList ordreListe;
	
	/**
	 * Default constructor
	 * @throws SQLException
	 */
	public LeveringsForm() throws SQLException {
		super(new BorderLayout());
		ordreVektor = RestaurantDB.getLeveringsOrdre();
		//ordreVektor = new Vector<Integer>();
		ordreListe = new JList(ordreVektor);
		//ordreListe.setSelectedIndex(0);
		ordreListe.setBorder(BorderFactory.createTitledBorder("Ordre"));
		ordreListe.setCellRenderer(new AlignedListCellRenderer(SwingConstants.CENTER));
		ordreListe.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ordreListe.getSelectionModel().addListSelectionListener(new OrdreSelectionListener());
		ordreListe.setPreferredSize(new Dimension(70, 600));
		
		
		add(ordreListe, BorderLayout.LINE_START);
		setVisible(true);
		Timer timer = new Timer();
		timer.schedule(new ListUpdateTimer(), 5000, 5000);
	}
	
	/**
	 * Change the entire info panel to match the selected order in the list
	 * @param listIdx Index of the wanted order in the list
	 * @throws SQLException
	 */
	void changeInfoPanel(int listIdx) throws SQLException {
		try {
			remove(1);
		} catch (ArrayIndexOutOfBoundsException e) {
		}
		
		LeveringsInfoForm lIPanel = new LeveringsInfoForm(ordreVektor.get(listIdx));
		add(lIPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Timer to poll changes from the DB
	 * @author Henning
	 *
	 */
	class ListUpdateTimer extends TimerTask {
		public void run() {
			try {
				ordreVektor = RestaurantDB.getLeveringsOrdre();
			} catch (SQLException e) {
			}
			ordreListe.setListData(ordreVektor);
		}
	}
	
	/**
	 * Selection listener for the order list
	 * @author Henning
	 *
	 */
	class OrdreSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (!e.getValueIsAdjusting()) {
				ListSelectionModel lsm = (ListSelectionModel) e.getSource();
				try {
					changeInfoPanel(lsm.getAnchorSelectionIndex());
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}

/**
 * List renderer which supports horizontal alignment
 * @author Henning
 *
 */
@SuppressWarnings("serial")
class AlignedListCellRenderer extends DefaultListCellRenderer {
    
    private int align;
    
    /**
     * Default constructor
     * @param align Alignment for the list elements
     */
    public AlignedListCellRenderer(int align) {
        this.align = align;
    }
 
    public Component getListCellRendererComponent(JList list, 
                                                  Object value, 
                                                  int index, 
                                                  boolean isSelected, 
                                                  boolean cellHasFocus) {
        JLabel lbl = (JLabel)super.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
        lbl.setHorizontalAlignment(align);
        return lbl;
    }
}